package com.orcsystem.product.service.impl;

import com.orcsystem.product.dao.CategoryBrandRelationDao;
import com.orcsystem.product.service.CategoryBrandRelationService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.orcsystem.common.utils.PageUtils;
import com.orcsystem.common.utils.Query;
import com.orcsystem.product.dao.BrandDao;
import com.orcsystem.product.entity.BrandEntity;
import com.orcsystem.product.service.BrandService;
import org.springframework.transaction.annotation.Transactional;

@Service("brandService")
public class BrandServiceImpl extends ServiceImpl<BrandDao, BrandEntity> implements BrandService {
    @Autowired
    CategoryBrandRelationService categoryBrandRelationService;
    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        // 1.获取key 如果key有值 模糊查询
        String key = (String) params.get("key");
        // 不为空
        QueryWrapper<BrandEntity> wrapper = new QueryWrapper<>();
        if (!StringUtils.isEmpty(key)){
            wrapper.eq("brand_id", key).or().like("name", key).or().like("descript",key);
        }

        System.out.println("key = " + key);
        IPage<BrandEntity> page = this.page(
                new Query<BrandEntity>().getPage(params),
                wrapper
        );
        return new PageUtils(page);
    }

    @Override
    @Transactional
    public void updateDetail(BrandEntity brand) {
        if (brand !=null){
            this.updateById(brand);
        }
        if (!StringUtils.isEmpty(brand.getName())){
            // 同步更新关联表 数据
            categoryBrandRelationService.updateBrand(brand.getBrandId(),brand.getName());

            // TODO 更新其他关联表 后续在补充完善
        }
    }

}